package com.shire.shire_interface.common.utils;

import com.shire.shire_interface.model.user.SystemUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

/**
 * 管理工具类
 */
public class ManageUtils {

    /**
     * 保存用户登录信息
     *
     * @param user
     */
    public static void saveUser(SystemUser user) {
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
        if (user != null) {
            session.setAttribute("sysUser", user);
        }
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    public static SystemUser getUser() {
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
        return (SystemUser) session.getAttribute("sysUser");
    }
    
    /**
     * 移除用户登录信息
     *
     * @param user
     */
    public static void removeUser() {
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
        session.removeAttribute("sysUser");
    }
}
